﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SimpleLibraryClasses;

namespace SimpleLibraryWinforms
{
    public partial class ChooseMemberDialog : Form
    {
        public ChooseMemberDialog(IEnumerable<Member> members)
        {
            InitializeComponent();
            this.Members = members;
            DisplayMembers();
        }

        private IEnumerable<Member> Members;

        public Member SelectedMember { get; set; }

        private void DisplayMembers()
        {
            foreach (Member member in this.Members)
            {
                ListViewItem item = new ListViewItem(member.FirstName);
                item.Tag = member;
                item.SubItems.Add(member.LastName);
                item.SubItems.Add(member.MemberId);
                membersListView.Items.Add(item);
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (membersListView.SelectedItems.Count != 1) return;
            this.SelectedMember = membersListView.SelectedItems[0].Tag as Member;
            this.DialogResult = DialogResult.OK;
        }

        private void membersListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (membersListView.SelectedItems.Count == 0) okButton.Enabled = false;
            else okButton.Enabled = true;
        }
    }
}
